﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SecsEquip;


namespace EquipSecs300
{
    public partial class RemoteControlView : CMyFormEx
    {        
        OnRemoteCommandExEventHandler m_rc;

        public RemoteControlView()
        {
            InitializeComponent();
        
            m_pViewName = "远程控制";
            m_rc = OnRemoteCommandThisThread;
                        
            listViewParament.Columns.Add("参数名", 100, HorizontalAlignment.Left);
            listViewParament.Columns.Add("参数值", 250, HorizontalAlignment.Left);
   
        }


        public override void UpdateData()
        {
            m_pSecs.m_pOnRemoteExCommand += OnRemoteCommandHandle;
            
        }

        // 响应远程命令处理
        void OnRemoteCommandHandle(object sender, RemoteCommandHandlerEventArgs e)
        {
            try
            {
                BeginInvoke(m_rc, sender, e); 
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            
        }

        // 本线程内处理
        void OnRemoteCommandThisThread(object sender, RemoteCommandHandlerEventArgs e)
        {                     
            REMOTE_REPLY reply = REMOTE_REPLY.eCOMMAND_PERFORMED;
            textBoxRCMD.Text = e.pCMD;

            listViewParament.Items.Clear();

            // 系统喜欢thorw错误
            try
            {
                foreach (var item in e.mapParament)
                {
                    string name = item.Key;
                    string value = item.Value;
                    AddListData(name, value);
                }
            }
            catch (System.Exception)
            {
                // 参数出错
                reply = REMOTE_REPLY.eCOMMAND_PARAMETER_IS_INVALID;
               
            }

            //m_pSecs.RemoteCmdReply(e.pReplyMailbox, reply); //由RemoteControl中回复给Host

        }


        // 添加数据
        void AddListData(string name, string value)
        {
            listViewParament.BeginUpdate();

            ListViewItem lvi = new ListViewItem();
            lvi.Text = name;

            lvi.SubItems.Add(value);
           
            listViewParament.Items.Add(lvi);

            listViewParament.EndUpdate();
        }

    }
}
